package com.mchsdk.paysdk.adapter;

import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.bitmap.callback.DefaultBitmapLoadCallBack;
import com.mchsdk.paysdk.bean.ChannelAndGameInfo;
import com.mchsdk.paysdk.entity.GamePackInfo;
import com.mchsdk.paysdk.http.process.PacksCodeProcess;
import com.mchsdk.paysdk.utils.MCHInflaterUtils;

import java.util.ArrayList;
import java.util.List;

public class MCPacksAdapter extends BaseAdapter {

    protected static final String TAG = "MCPacksAdapter";

    Context ctx;
    Activity activity;
    Handler mHandler;
    /**
     * 礼包列表
     */
    private List<GamePackInfo> packList = new ArrayList<GamePackInfo>();

    /**
     * 图片加载
     */
    BitmapUtils bitmapUtils;

    private LayoutInflater mInflater;

    public MCPacksAdapter(Activity activity, Context ctx, List<GamePackInfo> packList, BitmapUtils bitmapUtils, Handler handler) {
        this.activity = activity;
        this.ctx = ctx;
        this.packList = packList;
        this.bitmapUtils = bitmapUtils;
        this.mHandler = handler;
        mInflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }

    @Override
    public int getCount() {
        return packList.size();
    }

    @Override
    public Object getItem(int position) {
        return packList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final GamePackInfo packInfo = packList.get(position);

        final ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = mInflater.inflate(MCHInflaterUtils.getLayout(ctx, "item_mch_packs"), null);
            viewHolder.ivPackImage = (ImageView) convertView.findViewById(
                    MCHInflaterUtils.getControl(ctx, "iv_mch_pack_image"));
            viewHolder.tvPackName = (TextView) convertView.findViewById(
                    MCHInflaterUtils.getControl(ctx, "txt_mch_pack_name"));
            viewHolder.tvPackEffective = (TextView) convertView.findViewById(
                    MCHInflaterUtils.getControl(ctx, "txt_mch_pack_effective"));
            viewHolder.tvPackDesc = (TextView) convertView.findViewById(
                    MCHInflaterUtils.getControl(ctx, "txt_mch_pack_desc"));
            viewHolder.btnReceivePack = (Button) convertView.findViewById(
                    MCHInflaterUtils.getControl(ctx, "btn_mch_receive_pack"));

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        bitmapUtils.display(viewHolder.ivPackImage,
//				"http://img4.imgtn.bdimg.com/it/u=197413303,2658395499&fm=15&gp=0.jpg"
                packInfo.getPackImageUrl()
                , new DefaultBitmapLoadCallBack<ImageView>());
//		bitmapUtils.display(viewHolder.ivPackImage, "http://img4.imgtn.bdimg.com/it/u=197413303,2658395499&fm=15&gp=0.jpg", new DefaultBitmapLoadCallBack<ImageView>(){
//
//			@Override
//			public void onLoadCompleted(ImageView container, String uri, Bitmap bitmap, BitmapDisplayConfig config,
//					BitmapLoadFrom from) {
//				super.onLoadCompleted(container, uri, bitmap, config, from);
//				if(null != bitmap){
//					MCLog.e(TAG, "bitmap not null");
//					viewHolder.ivPackImage.setImageBitmap(bitmap);
//				}else{
//					MCLog.e(TAG, "bitmap is null");
//					viewHolder.ivPackImage.setImageResource(MCHInflaterUtils.getDrawable(ctx, "pack_image_new"));
//				}
//			}
//
//		});


        viewHolder.tvPackName.setText(ChannelAndGameInfo.getInstance().getGameName() + ":" + packInfo.getPackName());
//		StringBuilder effectDates = new StringBuilder();
//		effectDates.append("剩余有效期：").append(packInfo.getEffectiveDates()).append("天");
        if (packInfo.getEndTime().equals("0")) {
            viewHolder.tvPackEffective.setText("有效期：长期有效");
        } else {
            viewHolder.tvPackEffective.setText("有效期：" + packInfo.getStartTime() + "至" + packInfo.getEndTime());
        }
        viewHolder.tvPackDesc.setText(packInfo.getPackDesc());

        viewHolder.btnReceivePack.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                PacksCodeProcess packCode = new PacksCodeProcess();
                packCode.setGiftId(packInfo.getId());
                packCode.setPackName(packInfo.getPackName());
                packCode.post(mHandler);

//				new ReceivePackDialog.Builder()
//				.setPackName(packInfo.getPackName())
//				.setPackCode(packInfo.getPackCode())
//				.setPackStatus(packInfo.getPackStatus())
//				.show(ctx, activity.getFragmentManager());
            }
        });

        return convertView;
    }

    static class ViewHolder {
        ImageView ivPackImage;
        TextView tvPackName;
        TextView tvPackEffective;
        TextView tvPackDesc;
        Button btnReceivePack;
    }

}
